CC=gcc
LD=ld
BOOTLDFILE=boot/boot.lds
LOADERLDFILE=loader/loader.lds
KERNELLDFILE=kernel/kernel.lds
EXTRACTLDFILE=extract/extract.lds
#CTOOBJFLAGS= -c -nostdinc -fno-stack-protector  -fno-builtin -I kernel/machine/386/include 
CTOOBJFLAGS= -Wall -O3 -fomit-frame-pointer -fno-stack-protector -fstrength-reduce -c -nostdinc   -fno-builtin -I kernel/include 
ASMTOOBJFLAGS= -c 
ELFTOBINFLAGS= -R .pdr -R .comment -R .note -S -O binary
OBJCOPY=objcopy
KERNELOBJS=kernel/arc/386/init/kernelstart.o kernel/ipc/send.o kernel/syscall/getpcb.o kernel/ipc/receive.o  kernel/syscall/activate.o kernel/driver/drivermanager/drivermanager.o kernel/arc/386/lib/page.o kernel/driver/fsdriver/fsdriver.o kernel/server/servermanager/servermanager.o kernel/artist.o kernel/arc/386/init/kernelinit.o kernel/arc/386/init/interrupt.o kernel/driver/keyboarddriver/keyboarddriver.o kernel/arc/386/lib/keyboard.o kernel/arc/386/lib/io.o kernel/arc/386/lib/ata.o kernel/arc/386/lib/memory.o kernel/arc/386/lib/stdio.o kernel/arc/386/lib/string.o kernel/lib/global.o kernel/lib/io.o kernel/lib/kernelinit.o kernel/lib/memory.o kernel/lib/stdio.o kernel/lib/string.o kernel/lib/transform.o  kernel/schedule/schedule.o kernel/syscall/systemcall.o kernel/ipc/wait.o kernel/ipc/notify.o kernel/server/mmserver/mmserver.o kernel/syscall/detach.o kernel/server/fsserver/fsserver.o   kernel/lib/fstool.o kernel/lib/calculator.o kernel/arc/386/lib/calculator.o kernel/lib/mmtool.o kernel/affair/affairmanager/affairmanager.o kernel/affair/processaffair/processaffair.o kernel/lib/processtool.o kernel/interface/interfacemanager/interfacemanager.o
KERNELOBJSFROMC=kernel/artist.o  kernel/ipc/send.o kernel/arc/386/lib/page.o kernel/syscall/getpcb.o kernel/arc/386/init/kernelinit.o kernel/arc/386/lib/ata.o kernel/syscall/activate.o kernel/driver/drivermanager/drivermanager.o kernel/driver/keyboarddriver/keyboarddriver.o kernel/arc/386/lib/keyboard.o  kernel/driver/fsdriver/fsdriver.o kernel/server/servermanager/servermanager.o kernel/arc/386/lib/io.o kernel/arc/386/lib/memory.o kernel/arc/386/lib/stdio.o kernel/arc/386/lib/string.o kernel/lib/global.o kernel/lib/io.o kernel/lib/kernelinit.o kernel/lib/memory.o kernel/lib/stdio.o kernel/lib/string.o kernel/lib/transform.o  kernel/schedule/schedule.o kernel/syscall/systemcall.o kernel/ipc/receive.o kernel/ipc/wait.o kernel/ipc/notify.o kernel/server/mmserver/mmserver.o kernel/syscall/detach.o kernel/server/fsserver/fsserver.o kernel/lib/fstool.o kernel/lib/calculator.o kernel/arc/386/lib/calculator.o kernel/lib/mmtool.o kernel/affair/affairmanager/affairmanager.o kernel/affair/processaffair/processaffair.o kernel/lib/processtool.o   kernel/interface/interfacemanager/interfacemanager.o
KERNELOBJSFROMASM=kernel/arc/386/init/kernelstart.o kernel/arc/386/init/interrupt.o

Artist.img:boot/boot.bin  kernel/kernel.bin  loader/loader.bin 
	@dd if=boot/boot.bin of=Artist.img bs=512 count=1  
	@dd if=/dev/zero of=Artist.img skip=1 seek=1 bs=512 count=2879
	@sudo mount -o loop Artist.img /mnt/floppy
	@sudo cp -fv loader/loader.bin  /mnt/floppy
	@sudo cp -fv kernel/kernel.bin  /mnt/floppy
	@sudo umount /mnt/floppy

boot/boot.bin:boot/boot.elf
	@$(OBJCOPY) $(ELFTOBINFLAGS) $< $@
boot/boot.elf:boot/boot.o
	$(LD) $< -o $@  -T$(BOOTLDFILE)
boot/boot.o:boot/boot.S
	$(CC) $(ASMTOOBJFLAGS) $< -o $@


kernel/kernel.bin:kernel/kernel.elf extract/extract.bin
	@dd if=/dev/zero of=kernel/kernel.bin bs=1024 count=32
	@dd if=extract/extract.bin of=kernel/kernel.bin conv=notrunc
	@dd if=kernel/kernel.elf of=kernel/kernel.bin bs=1024 seek=32
	@du -b kernel/kernel.bin|gawk -f loader/GetSize.awk >loader/KernelSize.tmp
extract/extract.bin:extract/extract.S
	$(CC) $(ASMTOOBJFLAGS) $< -o extract/extract.o
	$(LD) -o extract/extract.elf  extract/extract.o  -T$(EXTRACTLDFILE)
	@$(OBJCOPY) $(ELFTOBINFLAGS) extract/extract.elf $@	
kernel/kernel.elf:$(KERNELOBJS)
	$(LD)  -o kernel/kernel.elf $^   -T$(KERNELLDFILE)
$(KERNELOBJSFROMC):%.o:%.c
	$(CC) $(CTOOBJFLAGS) $< -o $@
$(KERNELOBJSFROMASM):%.o:%.S
	$(CC) $(ASMTOOBJFLAGS) $< -o $@

loader/loader.o:loader/loader.S
	$(CC) $(ASMTOOBJFLAGS)   $< -o $@
loader/loader.elf:loader/loader.o
	$(LD) $< -o $@  -T$(LOADERLDFILE)
loader/loader.bin:loader/loader.elf
	@$(OBJCOPY) $(ELFTOBINFLAGS) $<  $@

clean:
	rm -f Artist.img
	cd boot && rm -f *.o *.bin *.elf *.tmp
	cd loader && rm -f *.o *.bin *.elf *.tmp
	cd extract && rm -f *.o *.bin *.elf *.tmp
	cd kernel && rm -f *.o *.bin *.elf *.tmp
	cd kernel/lib && rm -f *.o *.bin *.elf *.tmp
	cd kernel/ipc && rm -f *.o *.bin *.elf *.tmp
	cd kernel/syscall &&  rm -f *.o *.bin *.elf *.tmp
	cd kernel/schedule && rm -f *.o *.bin *.elf *.tmp
	cd kernel/driver/drivermanager && rm -f *.o *.bin *.elf *.tmp
	cd kernel/driver/keyboarddriver && rm -f *.o *.bin *.elf *.tmp	
	cd kernel/driver/fsdriver && rm -f *.o *.bin *.elf *.tmp
	cd kernel/server/servermanager     && rm -f *.o *.bin *.elf *.tmp
	cd kernel/server/mmserver     && rm -f *.o *.bin *.elf *.tmp
	cd kernel/arc/386/init && rm -f *.o *.bin *.elf *.tmp 
	cd kernel/arc/386/lib && rm -f *.o *.bin *.elf *.tmp
	cd kernel/server/fsserver && rm -f *.o *.bin *.elf *.tmp
	cd kernel/affair/affairmanager && rm -f  *.o  *.elf  *.bin  *.tmp
	cd kernel/affair/processaffair && rm -f *.o *.elf  *.bin  *.tmp
